#include "method.h"
#include "def.h"
#include <QSettings>
#include <QStringList>
#include <QChar>

QStringList multis(const QString &aa){
    QStringList list;
    // zero simbol is delemiter
    QChar x=aa.at(0);
    //qDebug()<<"Delemiter="<<x;
    // last delemiter found
    int last=1; //aa.indexOf(x,1);
    int next=aa.indexOf(x,last+1);
    int count = aa.mid(last,next-last).toInt();
    if(count==0 or aa.size()<=2){
        return list;
    }
    //qDebug()<<"last next Count="<<last<<next<<count;
    last=next;
    for (int i = 0 ; i < count -1 ; i++){
        next=aa.indexOf(x,last+1);
        list.append(aa.mid(last+1,next-last-1));
        last=next;
    }
    list.append(aa.mid(last+1,aa.size()-last));
    return list;
}

Method::Method(QObject *parent) :
    QObject(parent)
{
    version="";
    platform="";
    name="";
    authors="";
    exec="";
    //params="";
    mtype="";
    desc="";
    dpath="";
}

Method::~Method(){

}

bool Method::load(const QString & dname){
    this->dpath=dname;
    QSettings * info = new QSettings(dname+"/method.info",QSettings::IniFormat);
    this->version=info->value("version").toString();
    if(this->version != cfgver ){
        return false;
    }
    this->platform=info->value("platform").toString();
    this->name=info->value("name").toString();
    this->authors=info->value("authors").toString();
    this->exec=info->value("exec").toString();
    this->params=multis(info->value("params").toString());
    this->mtype=info->value("type").toString();
    this->desc=info->value("desc").toString();
    delete info;
    return true;
}
